package com.qcby.pushservice.base.moudle;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.qcby.framework.common.enums.DeleteFlagEnum;
import com.qcby.framework.common.exception.ServiceException;
import com.qcby.pushservice.base.bo.SendSmsBO;
import com.qcby.pushservice.base.mapper.MessageTemplateMapper;
import com.qcby.pushservice.base.mapper.SmsRecordMapper;
import com.qcby.pushservice.base.po.MessageTemplatePO;
import com.qcby.pushservice.base.po.SmsRecordPO;
import com.qcby.pushservice.common.enums.SmsStatusEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 发送短信
 *
 * @author cong.zhen
 * @date 2023/05/09
 */
@Slf4j
@Service
public abstract class AbstSendSms {

    private final String PATTERN_PHONE = "^1[3-9]\\d{9}$";

    public HashMap<String,AbstSendSms> channelMap = new HashMap<>(4);

    @Resource
    private MessageTemplateMapper messageTemplateMapper;

    @Resource
    private SmsRecordMapper smsRecordMapper;


    /**
     * 初始化
     */
    public abstract void init();

    public boolean pre(@Valid SendSmsBO sendSmsBO){
        checkParam(sendSmsBO);
        String sendDate = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
        SmsRecordPO smsRecordPO = SmsRecordPO.builder()
                .mobilePhone(sendSmsBO.getPhone())
                .sendCount(1)
                .status(SmsStatusEnums.SUBMIT_SUCCESS.getCode())
                .sendDate(sendDate)
                .build();
        smsRecordMapper.insert(smsRecordPO);
        /**发送mq消息**/
        log.info("发送短信应用:{},发送短信手机号:{}",sendSmsBO.getBusinessCode(),sendSmsBO.getPhone());
        return Boolean.TRUE;
    }


    /**
     * 发送
     *
     * @return {@link SendSmsBO}
     */
    public abstract SendSmsBO send();


    public void checkParam(SendSmsBO sendSmsBO){
        /**手机号是否正确**/
        if(!Pattern.matches(PATTERN_PHONE, sendSmsBO.getPhone())){
            throw new ServiceException("500","手机号格式不正确");
        }
        /**消息模板是否存在**/
        MessageTemplatePO messageTemplatePO = messageTemplateMapper.selectOne(new LambdaQueryWrapper<MessageTemplatePO>().eq(MessageTemplatePO::getMessageCode, sendSmsBO.getMessageCode()));
        if(ObjectUtil.isNull(messageTemplatePO)){
            throw new ServiceException("500","消息模板不存在");
        }
        if(DeleteFlagEnum.DELETE.getCode().equals(messageTemplatePO.getDelFlag()) || DeleteFlagEnum.DELETE.getCode().equals(messageTemplatePO.getStatus())){
            throw new ServiceException("500","消息模板不存在");
        }
        if(!messageTemplatePO.getBusinessCode().equals(sendSmsBO.getBusinessCode())){
            throw new ServiceException("500","应用和模板不匹配");
        }
        List<String> aa = new ArrayList<>();
        if(CollectionUtil.isEmpty(aa)){

        }
        if(StrUtil.isBlank(sendSmsBO.getPhone())){

        }
        List<Integer> list = Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8);
        List<Integer> collect = list.stream().distinct().collect(Collectors.toList());
        System.out.println(collect);
    }

}
